#install packages
install.packages("haven")
install.packages("DirectEffects")
install.packages("car")

require(haven)
require(DirectEffects)
require(car)

###############################################
#Appendix D
###############################################

#Table D10
##Note: Although Table D10 uses bootstraping (via boots_g() function in the DirectEffects package), this function
##does not offer an option to specify a seed. Therefore, the reproduction of results may exhibit slight differences 
##from those in online appendix D. However, these discrepancies do not fundamentally alter my conclusion that
##the direct effect of interest (ATE-ACDE) remains statistically significant.

rm(list=ls())
data <- read_dta("./Ghana_event.dta")

#Recentering the mediate variable to zero
data$norm_solidarity_last <- scale(data$solidarity_last)

#ATE
form_main_a <- pre_bi ~ v33 + v4 + v5 + v28 + logdis + loggpop + ashanti + gc 
direct_a <- sequential_g(form_main_a, data=data)  
summary(direct_a)
boots_a <- boots_g(direct_a)
summary(boots_a, level = 0.95)

#ACDE
form_main <- pre_bi ~ v33 + v4 + v5 + v28 + logdis + loggpop + ashanti + gc | elf + incumbent + mal + newcons + logdist + logpop + logeco + urban + pev_last | norm_solidarity_last
direct <- sequential_g(form_main, data = data)
confint(direct, "v33", level = 0.9)
summary(direct)
boots <- boots_g(direct)
summary(boots, level = 0.95)

#ATE-ACDE
difference <- boots_a[,2] - boots[,2]
result <- t.test(difference, mu=0)
print(result)
